define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config', '$calculater',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $calculater) {
            
            $scope.init = function(){
                $scope.loading_time = moment().format('YYYY-MM-DD HH:mm:ss');
                // 當月 request
                $scope.month_content = {
                    area_analysis_condition:[
                        { dateType: 'mo', moduelType: 'area_data' }
                    ]
                };
                // 當週 request
                $scope.week_content = {
                    area_analysis_condition:[
                        { dateType: 'wk', moduelType: 'area_data' }
                    ]
                };
                init_oneMonth();
            };
            
            $scope.back = function(){
                $rootScope.changeProgram('menu', {}, true);
            };

            // 初始載入當月區域資料
            function init_oneMonth(){
                loadAreaData($scope.month_content, 'Month');
            };

            // 更新
            $scope.upDate = function(){
                $scope.loading_time = moment().format('YYYY-MM-DD HH:mm:ss');
                if($scope.nowState == 'Month'){
                    loadAreaData($scope.month_content, 'Month');
                } else if($scope.nowState == 'Week'){
                    loadAreaData($scope.week_content, 'Week');
                }
            };

            // 當月
            $scope.getOneMonth = function(){
                loadAreaData($scope.month_content, 'Month');
            };
            
            // 當週
            $scope.getOneWeek = function(){
                loadAreaData($scope.week_content, 'Week');
            };

            // 載入區域資料
            function loadAreaData(content, nowState){
                getAllAreaData(content, function(data){
                    if(data && data.area_data.length > 0){
                        $scope.nowState = nowState;
                        $scope.areaDataList = data.area_data;
                        for(var i = 0; i < $scope.areaDataList.length; i++){
                            $scope.areaDataList[i].pieChart_config = {
                                innerRadiusRate:0.7,
                                isShowLabel:false,
                                radius:50
                            };
                            $scope.areaDataList[i].loading_op_qty = [{color:'rgb(73, 168, 238)', value:0},{color:'rgb(239, 239, 239)', value:0}];
                            $scope.areaDataList[i].loading_op_qty[0].value =  $scope.areaDataList[i].oee.toFixed(2);
                            $scope.areaDataList[i].loading_op_qty[1].value = 100 - $scope.areaDataList[i].loading_op_qty[0].value;
                            if($scope.areaDataList[i].loading_op_qty[0].value >= 100){
                                $scope.areaDataList[i].loading_op_qty[0].color = 'rgb(104, 189, 132)';
                            }
                        }; 
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    };
                });
            };

            // 點擊區域取得設備資料
            $scope.openEquipmentData = function(item){
                var content = {
                    area_analysis_condition:[
                        { dateType: 'mo', moduelType: 'equipment_data', area_no: item.area_no }
                    ]
                };
                getAllAreaData(content, function(data){
                    if(data && data.equipment_data.length > 0){
                        if(!item.equipmentData){
                            item.equipmentData = data.equipment_data || [];
                            item.style = {'display':'block'};
                        } else {
                            item.equipmentData = undefined;
                            item.style = {'display':'none'};
                        };
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    };
                });
            };

            // 取得所有區域的資料
            function getAllAreaData(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.area_analysis_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

        }
    ];
});
